package com;

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

@ChannelHandler.Sharable//1.标记这个类的实例可以在 channel 里共享
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
    private SerialPortManager spm = new SerialPortManager();

    // 激活串口
    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        spm.openSerialPort();
        spm.restart();
    }

    @Override
    public void channelRead0(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
        System.out.println("Client received: " + in.toString(CharsetUtil.UTF_8)); //3.打印接收到的信息
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();//4.打印异常堆栈跟踪
        ctx.close();//5.关闭通道
    }

    public void addChannel(Channel channel) {
        spm.addChannel(channel);
    }
}